using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace HelloXNA2.Component.Control
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class KeyboardControl : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch _spriteBatch;

        Texture2D _sprite;

        Vector2 _spritePosition;

        int _spriteSpeed = 6;

        public KeyboardControl(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        protected override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            _sprite = Game.Content.Load<Texture2D>("Image/Son");
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            KeyboardState keyboardState = Keyboard.GetState();

            if (keyboardState.IsKeyDown(Keys.Left) && _spritePosition.X >= 0)
                _spritePosition.X -= _spriteSpeed;
            if (keyboardState.IsKeyDown(Keys.Right) && _spritePosition.X <= Game.Window.ClientBounds.Width - _sprite.Width)
                _spritePosition.X += _spriteSpeed;
            if (keyboardState.IsKeyDown(Keys.Up) && _spritePosition.Y >= 0)
                _spritePosition.Y -= _spriteSpeed;
            if (keyboardState.IsKeyDown(Keys.Down) && _spritePosition.Y <= Game.Window.ClientBounds.Height - _sprite.Height)
                _spritePosition.Y += _spriteSpeed; 


            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            Game.GraphicsDevice.Clear(Color.CornflowerBlue);

            _spriteBatch.Begin();

            _spriteBatch.Draw(_sprite, _spritePosition, Color.White); 

            _spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
